package com.android.mybase.base

import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<T : ViewBinding> : Fragment() {
    lateinit var binding: T

    abstract fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): T

    companion object {
        inline fun <reified A : BaseActivity<*>> goTo(context: Context) {
            val intent = Intent(context, A::class.java)
            context.startActivity(intent)
        }

        // 如果需要传递额外参数
        inline fun <reified A : BaseActivity<*>> goTo(
            context: Context,
            block: Intent.() -> Unit
        ) {
            val intent = Intent(context, A::class.java)
            intent.block()
            context.startActivity(intent)
        }
    }

    fun setPadding(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v: View, insets: WindowInsetsCompat ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(0, systemBars.top, 0, 0)
            insets
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val auto = SystemBarStyle.auto(Color.BLACK, Color.BLACK) { resources: Resources? -> false }
        requireActivity().enableEdgeToEdge(auto, auto)
        binding = getBinding(inflater, container)
        return binding.root
    }

    /*    override fun onDestroyView() {
            super.onDestroyView()
            binding = null
        }*/
}